/*
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prepocessing_2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author baga
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    private File filePath;

    public Main(File files) {
        filePath = files;
    }

    private void contXMLFile(String filename, HashMap prop) {
        Element root = new Element("cran");
        Document doc = new Document(root);

        Set propSet = prop.keySet();
        Iterator iterProp = propSet.iterator();
        while (iterProp.hasNext()) {
            String propName = (String) iterProp.next();
            String propVal = (String) prop.get(propName);
            Element elm = new Element(propName);
            elm.setText(propVal);
            root.addContent(elm);
        }

        XMLOutputter outputer = new XMLOutputter();
        try {
            FileOutputStream output = new FileOutputStream(filename);
            outputer.output(doc, output);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void explores() {
        try {
            BufferedReader in = new BufferedReader(new FileReader(filePath));
            String s;
            int i = 0;
            StringBuilder sb = new StringBuilder();
            String subVal = null;
            String fileName = null;
            int curr = 0;
            HashMap hm = new HashMap();
            while ((s = in.readLine()) != null) {
                if (!s.isEmpty()) {
                    subVal = s.substring(0, 2);
                    if (subVal.equals(".I")) {
                        hm.put(elemTag(curr), sb.toString());
                        if (fileName != null) {
                            System.out.println(fileName);
                            contXMLFile(fileName + ".xml", hm);
                            removeHm(hm);
                        }
                        fileName = s.substring(3).trim();
                        sb.delete(0, sb.length());
//                        curr = 0;
                    } else if (subVal.equals(".W")) {
                        hm.put(elemTag(curr), sb.toString());
                        System.out.println(elemTag(curr) + " : " + sb.toString());
                        sb.delete(0, sb.length());
                        curr = 1;
                    } else if (subVal.equals(".T")) {
                        hm.put(elemTag(curr), sb.toString());
                        System.out.println(elemTag(curr) + " : " + sb.toString());
                        sb.delete(0, sb.length());
                        curr = 2;
                    } else if (subVal.equals(".A")) {
                        hm.put(elemTag(curr), sb.toString());
                        System.out.println(elemTag(curr) + " : " + sb.toString());
                        sb.delete(0, sb.length());
                        curr = 3;
                    } else {
                        sb.append(s + "\n");
                    }
                }

            }
            if (fileName != null) {
                System.out.println(fileName);
                contXMLFile(fileName + ".xml", hm);
                removeHm(hm);
            }
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }

    private void removeHm(HashMap hm) {
        Set setHm = hm.keySet();
        Object[] arrayKey = setHm.toArray(new Object[setHm.size()]);
        for (int i = 0; i < arrayKey.length; i++) {
            hm.remove(arrayKey[i]);
        }
    }

    public String elemTag(int curr) {
        String tag = null;
        switch (curr) {
            case 0:
                tag = "index";
                break;
            case 1:
                tag = "work";
                break;
            case 2:
                tag = "title";
                break;
            case 3:
                tag = "author";
                break;
        }
        return tag;
    }

    public static void main(String[] args) {
        Main app = new Main(new File("/export/home/baga/Documents/Book/TA/adi_coll/adi.all"));
        app.explores();
    }
}
